﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Unis.Framework.Core.Log;

namespace Unis.Framework.Core.Exceptions
{
    [Serializable, DebuggerStepThrough]
    public class APPException : ApplicationException
    {
        // Fields
        private string _ErrorCode;
        private APPMessageType _MsgLever;

        // Methods
        public APPException()
        {
            this._MsgLever = APPMessageType.OtherSysInfo;
        }

        public APPException(Exception innerException)
            : this(string.Empty, APPMessageType.SysErrInfo, innerException)
        {
        }

        public APPException(string clientMsg)
            : this(clientMsg, APPMessageType.SysErrInfo)
        {
        }

        public APPException(string clientMsg, APPMessageType msgLever)
            : this(clientMsg, msgLever, "-1")
        {
        }

        public APPException(string clientMsg, APPMessageType msgLever, Exception innerException)
            : base(clientMsg, innerException)
        {
            this._MsgLever = APPMessageType.OtherSysInfo;
            this._MsgLever = msgLever;
            string msgStr = clientMsg;
            if (innerException != null)
            {
                string errMsg = string.Empty;
                if (innerException is APPException)
                {
                    APPException exception = innerException as APPException;
                    this._MsgLever = exception.MsgLever;
                    errMsg = exception.Message;
                }
                else
                {
                    try
                    {
                        this.getErrMessage(innerException, ref errMsg);
                    }
                    catch
                    {
                    }
                }
                TraceEx.Write(errMsg, APPMessageType.SysErrInfo);
            }
            TraceEx.Write(msgStr, msgLever);
        }

        public APPException(string clientMsg, APPMessageType msgLever, string errorCode)
            : base(clientMsg)
        {
            this._MsgLever = APPMessageType.OtherSysInfo;
            this._MsgLever = msgLever;
            this._ErrorCode = errorCode;
            TraceEx.Write(clientMsg, msgLever);
        }

        private void getErrMessage(Exception ex, ref string errMsg)
        {
            errMsg = errMsg + " " + ex.Message;
            if (ex.InnerException != null)
            {
                this.getErrMessage(ex.InnerException, ref errMsg);
            }
        }

        // Properties
        public string ErrorCode
        {
            get
            {
                return this._ErrorCode;
            }
        }

        public APPMessageType MsgLever
        {
            get
            {
                return this._MsgLever;
            }
            set
            {
                this._MsgLever = value;
            }
        }
    }


    public enum APPMessageType
    {
        [Description("代码运行的信息")]
        CodeRunInfo = 4,
        [Description("数据验证")]
        DataInvalid = 7,
        [Description("显示给用户的异常信息")]
        DisplayToUser = 3,
        [Description("其它系统异常")]
        OtherSysInfo = 6,
        [Description("数据库操作异常")]
        SysDatabaseInfo = 1,
        [Description("系统异常")]
        SysErrInfo = 0,
        [Description("硬盘文件操作异常")]
        SysFileInfo = 2,
        [Description("系统警告")]
        SysWarning = 5
    }
}
